package com.donn.securitycontrolplatform.equipment.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donn.securitycontrolplatform.common.Result;
import com.donn.securitycontrolplatform.db1.mapper.TEquipmentMapper;
import com.donn.securitycontrolplatform.db2.mapper.TaginfoMapper;
import com.donn.securitycontrolplatform.equipment.entity.EquipmentVo;
import com.donn.securitycontrolplatform.equipment.entity.TEquipment;
import com.donn.securitycontrolplatform.equipment.entity.TGroup;
import com.donn.securitycontrolplatform.equipment.service.ITEquipmentService;
import com.donn.securitycontrolplatform.equipment.service.ITGroupService;
import com.donn.securitycontrolplatform.taginfo.entity.Taginfo;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglx
 * @since 2022-06-19
 */
@Service
@Slf4j
public class TEquipmentServiceImpl extends ServiceImpl<TEquipmentMapper, TEquipment> implements ITEquipmentService {
    @Autowired
    private TEquipmentMapper equipmentMapper;

    @Autowired
    private ITGroupService groupService;

    /**
     * 查询设备列表
     *
     * @param tEquipment 查询条件
     * @param page       分页
     * @return
     */
    @Override
    public Result selectTEquipmentList(TEquipment tEquipment, Page page) {
        try {
            QueryWrapper<TEquipment> queryWrapper = new QueryWrapper<>();
            if (StringUtil.isNotEmpty(tEquipment.getEquipmentNum())) {
                queryWrapper.lambda().eq(TEquipment::getEquipmentNum, tEquipment.getEquipmentNum());
            }
            if (StringUtil.isNotEmpty(tEquipment.getTagId())) {
                queryWrapper.lambda().eq(TEquipment::getTagId, tEquipment.getTagId());
            }
            if (StringUtil.isNotEmpty(tEquipment.getRemark())) {
                queryWrapper.lambda().eq(TEquipment::getRemark, tEquipment.getRemark());
            }
            IPage<TEquipment> equipmentIPage = page(page, queryWrapper);
            List<EquipmentVo> EquipmentVoList = new ArrayList<>();
            for (int i = 0; i < equipmentIPage.getRecords().size(); i++) {
                EquipmentVo vo = new EquipmentVo();
                TEquipment equipment = equipmentIPage.getRecords().get(i);
                BeanUtils.copyProperties(equipment, vo);
                TGroup group = groupService.getById(equipment.getGroupId());
                if (group != null) {
                    vo.setGroupName(group.getGroupName());
                }
                EquipmentVoList.add(vo);
            }
            IPage<EquipmentVo> equipmentVoPage = new Page<>();
            equipmentVoPage.setRecords(EquipmentVoList);
            equipmentVoPage.setPages(equipmentIPage.getPages());
            equipmentVoPage.setCurrent(equipmentIPage.getCurrent());
            equipmentVoPage.setSize(equipmentIPage.getSize());
            equipmentVoPage.setTotal(equipmentIPage.getTotal());
            return Result.success(equipmentVoPage);
        } catch (Exception e) {
            log.error("query error", e);
            return Result.init(500, "service error", e.getMessage());
        }
    }

    /**
     * 查询设备详情
     *
     * @param equipmentId 设备id
     * @return 详情
     */
    @Override
    public Result geTEquipmentById(String equipmentId) {
        try {
            return Result.success(getById(equipmentId));
        } catch (Exception e) {
            log.error("An error occurred in the getUserTagById : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    /**
     * 删除设备
     *
     * @param equipmentId 设备id
     * @return 删除结果
     */
    @Override
    public Result deleteTEquipment(String equipmentId) {
        try {
            TEquipment tEquipment = getById(equipmentId);
            if (tEquipment == null) {
                return Result.init(500, "该设备不存在", null);
            }
            Taginfo taginfo = new Taginfo();
            taginfo.setUsed(0);
            taginfo.setKind(0);
            taginfo.setBindid(0);
            UpdateWrapper<Taginfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Taginfo::getTagid, tEquipment.getTagId());
            taginfoMapper.update(taginfo, updateWrapper);
            return Result.success(removeById(equipmentId));
        } catch (Exception e) {
            log.error("An error occurred in the deleteUserTag : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    /**
     * 更新设备详情
     *
     * @param tEquipment 更新设备
     * @return
     */
    @Override
    public Result updateTEquipment(TEquipment tEquipment) {
        try {
            LocalDateTime localDateTime = LocalDateTime.now();
//            tEquipment.setUpdateTime(localDateTime);
//            tEquipment.setOperateTime(localDateTime);
            tEquipment.setStatus("已处理");
            return Result.success(saveOrUpdate(tEquipment));
        } catch (Exception e) {
            log.error("An error occurred in the insertTEquipment : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    /**
     * 新增设备
     *
     * @param tEquipment 设备
     * @return 新增结果
     */
    @Autowired
    TaginfoMapper taginfoMapper;

    @Override
    public Result insertTEquipment(TEquipment tEquipment) {
        try {
            LocalDateTime localDateTime = LocalDateTime.now();
//            tEquipment.setTime(localDateTime);
            tEquipment.setOperateTime(localDateTime);
//            tEquipment.setTime(LocalDateTime.now());
            tEquipment.setStatus("未处理");
            saveOrUpdate(tEquipment);

            Taginfo taginfo = new Taginfo();
            taginfo.setUsed(1);
            taginfo.setKind(2);
            taginfo.setStatus(1);
            taginfo.setBindid(tEquipment.getId());
            UpdateWrapper<Taginfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(Taginfo::getTagid, tEquipment.getTagId());
            taginfoMapper.update(taginfo, updateWrapper);
            return Result.success(tEquipment);
        } catch (Exception e) {
            log.error("An error occurred in the insertTEquipment : ", e);
            return Result.init(500, "Service error", e.getMessage());
        }
    }

    @Override
    public Result getQuantityBy(LocalDateTime startTime, LocalDateTime endTime) {
        List<String> tagIds = equipmentMapper.getTagIds("高压气瓶");
        return null;
    }
}
